package cn.tedu.eb_film.utils;

import io.jsonwebtoken.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * JWT工具类
 *
 * @author ライナー・ロンテ
 * @since 2023/3/8 11:41
 */
@Component
public class JwtUtils {
    public static String createJwt(Map<String,Object> claims,String secretKey,Long survivalTime) {
        return Jwts.builder()
                .setHeaderParam("alg", "HS256")
                .setHeaderParam("typ", "JWT")
                .setClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis() + survivalTime * 1000 * 60))
                .signWith(SignatureAlgorithm.HS256,secretKey)
                .compact();
    }

    public static Claims parse(String jwt,String secretKey) throws ExpiredJwtException,UnsupportedJwtException
            ,MalformedJwtException,SignatureException,IllegalArgumentException{

            return Jwts.parser().setSigningKey(secretKey)
                    .parseClaimsJws(jwt)
                    .getBody();
    }
}
